package com.lds.sign_sdk.crypto;

import com.lds.sign_sdk.emnu.Algorithm;
import com.lds.sign_sdk.util.AESUtils;
import com.lds.sign_sdk.util.CryptoUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.invoke.MethodHandles;

/**
 * @Date 2022/7/29
 * @Time 16:08
 * @Author liudongs
 * @E-mail liudongs@aliyun.com
 * @Version 1.0.0
 * @Description
 **/
public class DefaultCrypto implements ICrypto{
    private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
    private static final String KEY = "edcb87b4-68b1-466b-8f6d-256ef53e50f0";

    public DefaultCrypto() {
    }

    @Override
    public String encrypt(Algorithm algorithm, String value, String key) throws Exception {
        if (key == null || key.length() == 0) {
            key = KEY;
        }

        String result;
        switch(algorithm) {
            case MD5:
                result = CryptoUtil.encryptBASE64(CryptoUtil.encryptMD5(value.getBytes()));
                break;
            case AES:
                result = AESUtils.encryptBase64(key, value);
                break;
            default:
                result = AESUtils.encryptBase64(key, value);
        }

        return result;
    }

    @Override
    public String decrypt(Algorithm algorithm, String value, String key) {
        if (key == null || key.length() == 0) {
            key = KEY;
        }

        String result;
        try {
            switch(algorithm) {
                case MD5:
                    logger.debug("该算法不支持解密");
                    result = "";
                    break;
                case AES:
                    result = AESUtils.decryptBase64(key, value);
                    break;
                default:
                    result = AESUtils.decryptBase64(key, value);
            }
        } catch (IllegalArgumentException var6) {
            logger.debug("值：‘" + value + "’不支持解密");
            result = "";
        }

        return result;
    }
}
